package lesson19.notepad;

import java.util.ArrayList;
import java.util.List;

public class SimpleNotepad implements INotepad{
	private List<Page> pages = new ArrayList<Page>(); 
	
	public SimpleNotepad() {}
	
	public void addPage(Page page)
	{
		if(page != null)
			this.pages.add(page);
	}
	
	@Override
	public void addTextToCurrentPage(final int pageNumber, final String text) {
		if(validatePage(pageNumber))
			this.pages.get(pageNumber).addText(text);
	}

	@Override
	public void replaceTextToCurrentPage(final int pageNumber, final String text) {
		if(validatePage(pageNumber))
			pages.get(pageNumber).deleteText();
			pages.get(pageNumber).addText(text);
	}

	
	@Override
	public void deleteTextAtCurrentPage(final Page selectedPage) {
		for (Page p : pages) {
				if(p.equals(selectedPage))
					p.deleteText();
		}
	}

	@Override
	public void showPages() {
		for (Page i : pages) {
			System.out.println(i.getPageInfo());
		}
		
	}
	
	private boolean validatePage(int pageNumber)
	{
		return pages.size() > pageNumber && pageNumber >= 0;
	}
	
}
